<?php // 4.0
class quote_specify {
  function loaddata() {
    global $masterclass;

    $masterclass->loadmodule('customers');
    $masterclass->loadmodule('personnel');
    $masterclass->loadmodule('invoices');
    $masterclass->loadmodule('companies');

    $masterclass->websiteconfig['title']='Invoice';
    
    $masterclass->site['postfields'][] = array(
      'table'=>'invoices_documents',
      'trackingid'=>'invoicesid',
      'fields'=>array(
        'documentid'=>'invoices_documentid',
        'recipientlineone'=>'invoices_recipientlineone'
      )
    );
    
    if ((isset($masterclass->sitevars['invstaticitemid']) && $masterclass->sitevars['invstaticitemid']!='') AND  (isset($masterclass->sitevars['invoicesid']) && $masterclass->sitevars['invoicesid']!='')) {
      $this->save_invoiceitem_static($masterclass->sitevars['invstaticitemid'],$masterclass->sitevars['invoicesid']);
      header("Location: ".$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory']."invoices/quote_add.php?".$masterclass->trackingvar_query());
    }
    
  return;
  }
  
  function savedata() {
    global $masterclass;

    foreach ($masterclass->sitevars['post_action'] as $postaction) {
      switch ($postaction) {
          
      }
    }

  return;
  }

  function save_invoiceitem_static($staticid='',$invoiceid='') {
    global $masterclass;

    $staticitem=$masterclass->allclasses['invoices']->fetch_invoices_staticitem($staticid);
    if (!empty($staticitem)) {
    $dbinfo = array(
      'table'=>'invoices_documentitems',
      'static'=>array(
        'qty'=>$staticitem[0]['qty'],
        'name'=>$staticitem[0]['name'],
        'description'=>$staticitem[0]['description'],
        'cost'=>$staticitem[0]['cost'],
        'taxed'=>$staticitem[0]['taxed'],
        'taxpercent'=>$staticitem[0]['taxpercent'],
        'personnel'=>$masterclass->site['personnelid'],
        'invoices'=>$invoiceid
      )
    );
    $masterclass->uploaddata($dbinfo);
    }
    
  return;
  }
  
  
  function display_invoices_itemslist($listarray=array()) {
    global $masterclass;
    $arrret=array();

    $ret='';
    if (empty($listarray)) {
      $ret.='No Items';
    } else {
      $arrinvoicetotal=$masterclass->allclasses['invoices']->calc_invoicetotal($listarray);

      foreach ($listarray as $result) {
        $pname='';
        $profile='';
        if ($listedids!='') { $listedids.=','; } $listedids.=$result['id'];

        if (date("Y-m-d")==date("Y-m-d",strtotime($result['updated']))) {
          $pstamp = date("H:m:s",strtotime($result['updated']));
        } else {
          $pstamp = date("Y-m-d",strtotime($result['updated']));
        }

        if (preg_match('/^[0-9.-]+$/iD', $result['qty'])) { $qty=$result['qty']; } else { $qty=1; }
        if (preg_match('/^[0-9.-]+$/iD', $result['cost'])) { $cost=$result['cost']; } else { $cost=0; }
        if (preg_match('/^[0-9.-]+$/iD', $result['taxpercent'])) { $taxpercent=$result['taxpercent']; } else { $taxpercent=0; }

        if ($cost<0) { $polarity='-'; } else { $polarity='+'; }

        $subtotal=$qty*abs($cost);
        $taxcost=$subtotal*($taxpercent/100);
	    $total=$taxcost+$subtotal;

        $profile_mouseover='OnMouseOver="document.getElementById(\'invoices_itemsbrief'.$result['id'].'\').className=\'sitebox_content_mouseover\';document.getElementById(\'invoices_itemsdesc'.$result['id'].'\').className=\'sitebox_content_mouseover\';" OnMouseOut="document.getElementById(\'invoices_itemsbrief'.$result['id'].'\').className=\'sitebox_content\';document.getElementById(\'invoices_itemsdesc'.$result['id'].'\').className=\'sitebox_content\';"';
        $profile_onclick_open='OnClick="openwindow(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/edititem_default.php?invoicesitemid='.$result['id'].'\',\'invoices_item'.$result['id'].'\',\'480\',\'360\');"';
        $removeitemclick='OnClick="if (confirm(\'Remove Item\\n'.$result['name'].'?\')) { invoices_remove_item(\'invoices_removeitem\',\''.$result['id'].'\'); }" ';

        $profile.='<a href="'.$removeitemclick.'"><img src="'.$masterclass->mainconfig['dir'].'art/cross.png" border="0" title=" Delete "></a>';

        $profile.=$result['qty'];
        $profile.='&nbsp;';
        if (preg_match('/^[0-9.-]+$/iD', $total)) { $profile.='$'.number_format($total,2); } else { $profile.=$total; }
        $profile.='&nbsp;';
        $profile.=$result['name'];

        if ($result['description']!='') {
          $profile.='<br>';
	      $profile.=nl2br($result['description']);
        }
        $profile.='<hr>';
        $ret.=$profile;
      }
      
      $ret.=number_format($arrinvoicetotal['qty'],0).' - Item Count<br>';
      $ret.='$'.number_format($arrinvoicetotal['subtotal'],2).' - SubTotal<br>';
      $ret.='$'.number_format($arrinvoicetotal['tax'],2).' - Tax<br>';
      $ret.='$'.number_format($arrinvoicetotal['total'],2).' - Total<br>';
      
    }

    $arrret['load_list']=$ret;
    $arrret['load_containername']=$containername;
    
    $masterclass->site['grabdata_vars']['load_list']=$ret;
    $masterclass->site['grabdata_vars']['load_containername']=$containername;

  return $arrret;
  }

  function display() {
    global $masterclass;

    $arrdocumentdates=$masterclass->allclasses['invoices']->fetch_invoicesdocumentdates($masterclass->sitevars['invoicesid']);
    $masterclass->sitevars['invoices_documentdate']=$arrdocumentdates[0];
    $masterclass->sitevars['invoices_duedate']=$arrdocumentdates[1];
    $masterclass->sitevars['invoices_paid']=$arrdocumentdates[2];
    
    if ($masterclass->sitevars['invoices_documentdate']=='0000-00-00' || $masterclass->sitevars['invoices_documentdate']=='') { $masterclass->sitevars['invoices_documentdate']=date("Y/m/d"); } else { $masterclass->sitevars['invoices_documentdate']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_documentdate'])); }
    if ($masterclass->sitevars['invoices_duedate']=='0000-00-00' || $masterclass->sitevars['invoices_duedate']=='') { $masterclass->sitevars['invoices_duedate']=''; } else { $masterclass->sitevars['invoices_duedate']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_duedate'])); }
    if ($masterclass->sitevars['invoices_paid']=='0000-00-00' || $masterclass->sitevars['invoices_paid']=='') { $masterclass->sitevars['invoices_paid']=''; } else { $masterclass->sitevars['invoices_paid']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_paid'])); }


    $arrinvoiceitems=$masterclass->allclasses['invoices']->fetch_invoices_items($masterclass->sitevars['invoicesid']);
    $arrinvoicetotal=$masterclass->allclasses['invoices']->calc_invoicetotal($arrinvoiceitems);
    
    echo '<b>'.$masterclass->sitevars['invoices_recipientlineone'].' #'.$masterclass->sitevars['invoices_documentid'].'</b><br>';
          
    echo "<b>";
    echo number_format($arrinvoicetotal['qty'],0).' - Item Count<br>';
    echo '$'.number_format($arrinvoicetotal['subtotal'],2).' - SubTotal<br>';
    echo '$'.number_format($arrinvoicetotal['tax'],2).' - Tax<br>';
    echo '$'.number_format($arrinvoicetotal['total'],2).' - Total<br>';
    echo "</b>";
    echo '<center><b>Select Item</b></center><br>';
    $staticitems=$masterclass->allclasses['invoices']->fetch_invoices_staticitems('default');
    if (!empty($staticitems)) {
      foreach ($staticitems as $item) {
        if ($item['qty']<1) { $itemqty=1; } else { $itemqty=$item['qty']; }
        $itemcost=$item['cost']*$itemqty;
        echo '<a href="quote_add.php?invstaticitemid='.$item['id'].'&'.$masterclass->trackingvar_query().'">'.$itemqty.'&nbsp;-&nbsp;$'.number_format($itemcost,2).'&nbsp;-&nbsp;'.$item['name'].'&nbsp;</a><br>';
      }
    }
    echo '<br>';
    echo '<input type="submit" value=" Create Invoice ">';
    echo '<br>';
    echo '<center><b>Specify An Item</b></center><br>';

    echo 'Qty&nbsp;'.$masterclass->allclasses['websites']->disp_inputtext('invoices_documentitem_new_qty','','8','6').'<br>';
    echo 'Cost Ea&nbsp;'.$masterclass->allclasses['websites']->disp_inputtext('invoices_documentitem_new_cost','','8','8').'<br>';
    echo 'Tax&nbsp;'.$masterclass->allclasses['websites']->disp_inputcheckbox('invoices_documentitem_new_taxed','1').'&nbsp;@&nbsp;'.$masterclass->allclasses['websites']->disp_inputtext('invoices_documentitem_new_taxpercent','','6','5').'%<br>';
    echo 'Item<br>'.$masterclass->allclasses['websites']->disp_inputtext('invoices_documentitem_new_name','95%').'<br>';
    echo 'Description<br>'.$masterclass->allclasses['websites']->disp_textarea('invoices_documentitem_new_description','95%','3','40').'<br>';

    echo '<center><b>Invoice Items</b></center><br>';
  
    $listdata=$this->display_invoices_itemslist($arrinvoiceitems);
    echo $listdata['load_list'];

  return;
  }
}
?>